package com.example.servlet;

import com.alibaba.fastjson.JSON;
import com.example.model.Admin;
import com.example.service.AdminService;
import com.example.service.impl.AdminServiceImpl;
import com.example.vo.ResponseData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/json;charset=utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        AdminService adminService = new AdminServiceImpl();
        Admin admin = adminService.login(username, password);
        ResponseData data = null;
        if(admin!= null){
            data = ResponseData.ok(admin);
        }else{
            data = ResponseData.fail();
        }
        resp.getWriter().println(JSON.toJSONString(data));
    }
}
